package com.glorysoft.hlgdpda.ui.trackin.view

import android.os.Bundle
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityTrackInBinding
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.trackin.viewmodel.NormalTrackInViewModel
import com.glorysoft.hlgdpda.ui.trackin.viewmodel.TrackInViewModel
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_track_in.*
import kotlinx.android.synthetic.main.activity_track_in.downMaterialEmptyView
import kotlinx.android.synthetic.main.activity_track_in.etCarrierDown
import kotlinx.android.synthetic.main.activity_track_in.etCarrierUp
import kotlinx.android.synthetic.main.activity_track_in.etEqpWorkID
import kotlinx.android.synthetic.main.activity_track_in.etLot
import kotlinx.android.synthetic.main.activity_track_in.rvDownMaterialList
import kotlinx.android.synthetic.main.activity_track_in.rvUpMaterialList
import kotlinx.android.synthetic.main.activity_track_in.tvState
import kotlinx.android.synthetic.main.activity_track_in.tvStep
import kotlinx.android.synthetic.main.activity_track_in.upMaterialEmptyView
import kotlinx.android.synthetic.main.activity_track_out.*

/**
 * FileName: TrackInActivity
 * Author: XJH
 * Date: 2021/5/14 11:56
 * Description:
 * History:
 */
class TrackInActivity : BaseActivity<NormalTrackInViewModel, ActivityTrackInBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_track_in
    }

    override fun initVariableId(): Int {
        return BR.normalTrackInViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        rvMaterielList.setEmptyView(MaterielEmptyView)
        rvUpMaterialList.setEmptyView(upMaterialEmptyView)
        rvDownMaterialList.setEmptyView(downMaterialEmptyView)
        setTitle("批次进站")
        setRightListener("提交") {
            viewModel.defaultTrackIn(etEqpWorkID.text.toString())
        }
    }

    override fun initData() {
        initEdit(etLot, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.searchLotInfo(s, etEqpWorkID.text.toString())
            }
        })

        initEdit(etEqpWorkID, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.searchMaterial(s)
            }
        })

        initEdit(etCarrierUp, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.searchCarrierInfo(s, 1)
            }

        })

        initEdit(etCarrierDown, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.searchCarrierInfo(s, 2)
            }

        })

        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                1 -> {
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this,
                        "确认删除",
                        "我再想想",
                        "是否确认删除该物料信息？",
                        {
                            viewModel.deleteInfo("material", it.item)
                        }) {}.show(supportFragmentManager, "DELETE_DIALOG")
                }

                2 -> {
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this,
                        "确认删除",
                        "我再想想",
                        "是否确认删除该载具信息？",
                        {
                            viewModel.deleteInfo("carrierUp", it.item)
                        }) {}.show(supportFragmentManager, "DELETE_DIALOG")
                }

                3 -> {
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this,
                        "确认删除",
                        "我再想想",
                        "是否确认删除该载具信息？",
                        {
                            viewModel.deleteInfo("carrierDown", it.item)
                        }) {}.show(supportFragmentManager, "DELETE_DIALOG")
                }

                200 -> {
                    val entity = it.item as LotEntity
                    etLot.setText(
                        entity.LOT.LOTID
                    )

                    tvStep.text = entity.LOT.STEPDESC

                    tvState.text = entity.LOT.STATE

                    etCarrierUp.setText("")
//                    etCarrierDown.setText("")
                }
            }
        }

        viewModel.defUI.startActivity.observe(this) {
            etLot.setText("")
            etEqpWorkID.setText("")
            tvStep.text = ""
            tvState.text = ""
            etCarrierUp.setText("")
            etCarrierDown.setText("")
        }
    }
}